//
//  YJCRegistLogin.m
//  xiangzhe
//
//  Created by lyt on 16/11/21.
//  Copyright © 2016年 com.dazhebao. All rights reserved.
//

#import "YJCRegistLogin.h"
#import "YTTimeButton.h"
#import "YJCIsMobile.h"

@interface YJCRegistLogin ()
@property (nonatomic, strong) UITextField *registNum;
@property (nonatomic, strong) UITextField *phoneNum;
@property (nonatomic, strong) UITextField *safeCode;
@property (nonatomic, strong) YTTimeButton *sendBtn;
@end

@implementation YJCRegistLogin

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_denglu"]];
    
    [self setupUI];

}

- (void)sendMessage{
    // 退出键盘
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    if (self.phoneNum.text.length == 11 && self.safeCode.text.length > 0) {
        [[YJCNetWork sharedNetWork] requestWithMethod:POST serviceName:@"User.Quick_login" parameters:@{@"phone":self.phoneNum.text,@"code":self.safeCode.text} finshed:^(id result, NSError *error) {
            NSLog(@"%@---%@",result,error);
            [SVProgressHUD showWithStatus:@"正在登录"];
        }];
    }else{
        [SVProgressHUD showImage:nil status:@"手机号或者验证码错误"];
        [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, KScreenH / 4)];
    }
    
}
- (void)loginBtnClick{
    // 退出键盘
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    if (self.phoneNum.text.length == 11 && self.safeCode.text.length > 0) {
        [[YJCNetWork sharedNetWork] requestWithMethod:POST serviceName:@"User.Code_login" parameters:@{@"phone":self.phoneNum.text,@"code":self.safeCode.text,@"active_code":self.registNum.text} finshed:^(id result, NSError *error) {
            NSLog(@"%@---%@",result,error);
            [SVProgressHUD showWithStatus:@"正在登录"];
            
            NSString *resultCode = result[@"data"][@"resultCode"];
            
            if (resultCode.intValue == 0) {
                [SVProgressHUD dismiss];
                NSLog(@"跳转到主页");
                [self dismissViewControllerAnimated:YES completion:^{
                    
                }];
                
            }else{
                NSString *mg = result[@"data"][@"resultInfo"];
                NSLog(@"网络错误 %@",mg);
                [SVProgressHUD showImage:nil status:mg];
            }
        }];
    }else{
        [SVProgressHUD showImage:nil status:@"手机号或验证码错误"];
        [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, KScreenH / 4)];
    }
    
}
- (void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)setupUI{
    //标题
    UILabel *titleLab = [[UILabel alloc] init];
    titleLab.text = @"注册码登录";
    titleLab.font = [UIFont systemFontOfSize:17];
    titleLab.textColor = [UIColor yt_colorWithHexString:@"#685d5d"];
    titleLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titleLab];
    //小标题
    UILabel *signLab = [[UILabel alloc] init];
    signLab.text = @"使用注册码登录";
    signLab.font = [UIFont systemFontOfSize:13];
    signLab.textAlignment = NSTextAlignmentCenter;
    signLab.textColor = [UIColor yt_colorWithHexString:@"#949494"];
    [self.view addSubview:signLab];
    
    //3条分割线
    UIView *topView = [[UIView alloc] init];
    UIView *leftView = [[UIView alloc] init];
    UIView*rightView = [[UIView alloc] init];
    topView.backgroundColor = [UIColor yt_colorWithHexString:@"#efefef"];
    leftView.backgroundColor = [UIColor yt_colorWithHexString:@"#efefef"];
    rightView.backgroundColor = [UIColor yt_colorWithHexString:@"#efefef"];
    [self.view addSubview:topView];
    [self.view addSubview:leftView];
    [self.view addSubview:rightView];
    
    //注册码
    UITextField *registNum = [[UITextField alloc] init];
    registNum.textColor = [UIColor yt_colorWithHexString:@"#685d5d"];
    registNum.placeholder = @"请输入注册码";
    registNum.font = [UIFont systemFontOfSize:13];
    
    registNum.layer.borderColor = [UIColor yt_colorWithHexString:@"#efefef"].CGColor;
    registNum.layer.borderWidth = 1.0;
    registNum.layer.cornerRadius = 3.0;
    registNum.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
    registNum.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:registNum];
    self.registNum = registNum;
    registNum.layer.borderColor = [UIColor yt_colorWithHexString:@"#efefef"].CGColor;
    registNum.tintColor = [UIColor blueColor];
//    registNum.keyboardType = UIKeyboardTypePhonePad;
    // 手机号
    UITextField *phoneNum = [[UITextField alloc] init];
    phoneNum.textColor = [UIColor yt_colorWithHexString:@"#685d5d"];
    phoneNum.placeholder = @"手机号";
    phoneNum.font = [UIFont systemFontOfSize:13];
    
    phoneNum.layer.borderColor = [UIColor yt_colorWithHexString:@"#efefef"].CGColor;
    phoneNum.layer.borderWidth = 1.0;
    phoneNum.layer.cornerRadius = 3.0;
    phoneNum.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
    phoneNum.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:phoneNum];
    self.phoneNum = phoneNum;
    phoneNum.tintColor = [UIColor blueColor];
    phoneNum.keyboardType = UIKeyboardTypePhonePad;
    // 验证码
    UITextField *safeCode = [[UITextField alloc] init];
    safeCode.textColor = [UIColor yt_colorWithHexString:@"#685d5d"];
    safeCode.placeholder = @"验证码";
    safeCode.font = [UIFont systemFontOfSize:13];
    
    safeCode.layer.borderColor = [UIColor yt_colorWithHexString:@"#efefef"].CGColor;
    safeCode.layer.borderWidth = 1.0;
    safeCode.layer.cornerRadius = 3.0;
    safeCode.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
    safeCode.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:safeCode];
    self.safeCode = safeCode;
    
    //发送验证码的按钮
    YTTimeButton *timeBtn = [YTTimeButton buttonWithType:UIButtonTypeCustom];
    [timeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [timeBtn setTitleColor:[UIColor yt_colorWithHexString:@"#685d5d"] forState:UIControlStateNormal];
    timeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    timeBtn.layer.borderColor = [UIColor yt_colorWithHexString:@"#ff3366"].CGColor;
    timeBtn.layer.borderWidth = 1.0;
    timeBtn.layer.cornerRadius = 3.0;
    [self.view addSubview:timeBtn];
    [timeBtn addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
    self.sendBtn = timeBtn;
    //手机号码登录按钮
    UIButton *phoneLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [phoneLoginBtn setTitle:@"手机号码登录" forState:UIControlStateNormal];
    [phoneLoginBtn setTitleColor:[UIColor yt_colorWithHexString:@"#ff3366"] forState:UIControlStateNormal];
    phoneLoginBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [phoneLoginBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:phoneLoginBtn];
    // 登录按钮
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:@"登录" forState: UIControlStateNormal];
    [loginBtn setBackgroundColor:[UIColor yt_colorWithHexString:@"#ff3366"]];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:loginBtn];
    loginBtn.alpha = 0.45;
    [loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    // 布局
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(20);
        make.centerX.equalTo(self.view);
    }];
    
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(60);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(KScreenW);
    }];
    
    [signLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView.mas_bottom).offset(kHdistance);
        make.centerX.equalTo(self.view);
    }];
    
    
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(1.5*kHdistance);
        make.centerY.equalTo(signLab);
        make.height.mas_equalTo(1);
        make.right.equalTo(signLab.mas_left).offset(-kVdistance);
    }];
    
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-1.5*kHdistance);
        make.centerY.equalTo(signLab);
        make.left.equalTo(signLab.mas_right).offset(kVdistance);
        make.height.mas_equalTo(1);
    }];
    
    [registNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kHdistance);
        make.right.equalTo(self.view).offset(-kHdistance);
        make.top.equalTo(signLab.mas_bottom).offset(kHdistance);
        make.height.mas_equalTo(30);
        
    }];
    
    [phoneNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kHdistance);
        make.top.equalTo(registNum.mas_bottom).offset(kHdistance);
        make.height.mas_equalTo(30);
    }];
    
    [timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-kHdistance);
        make.centerY.equalTo(phoneNum);
        make.left.equalTo(phoneNum.mas_right).offset(kVdistance);
        make.width.mas_equalTo(80);
    }];

    [safeCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneNum.mas_bottom).offset(2*kVdistance);
        make.left.equalTo(self.view).offset(kHdistance);
        make.right.equalTo(self.view).offset(-kHdistance);
        make.height.mas_equalTo(30);
    }];
    
    [phoneLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(safeCode.mas_bottom).offset(2*kVdistance);
        make.left.equalTo(self.view).offset(kHdistance);
        
    }];
    
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneLoginBtn.mas_bottom).offset(2*kVdistance);
        make.left.equalTo(self.view).offset(kHdistance);
        make.right.equalTo(self.view).offset(-kHdistance);
    }];
 
    
}
@end
